var limsadminUrl = "/solr/select/?q=limsadminKey:dudley_limsadminkey&wt=json&fl=" 
+ "limsadminKey,limsadminYoCount,limsadminYoMaxNum,limsadminYoBoxNum,limsadminYoPosition,limsadminYadCount,limsadminYadMaxNum,limsadminYadBoxNum,limsadminCrossingCount,limsadminYadPosition,limsadminYpgCount,limsadminYpgMaxNum,limsadminYpgBoxNum,limsadminYpgPosition,limsadminChemCount,limsadminChemMaxNum,limsadminDOligoMaxNum,limsadminDOligoCount,limsadminLOligoMaxNum,limsadminLOligoCount,limsadminPlasmidMaxNum,limsadminPlasmidCount,limsadminPlasmidBoxNum,limsadminYseqCount,limsadminYseqOligoCount,limsadminYseqRADCount,limsadminYcgsCount";
var limsadmin_obj = {};

function getSolrSearchUrl(searchTerm, maxresults, fields) {
    return "/solr/select/?q=" + searchTerm + "&wt=json&rows=" + maxresults + "&fl=" + fields;
}

function getSolrLimsAdmin(callback){
Ext.Ajax.request({
                    url: limsadminUrl,
                    method: "GET",
                    success: function(response) {
                        var resp = Ext.util.JSON.decode(response.responseText);
                        var adminObj = resp.response.docs[0];//Ext.util.JSON.decode(response.responseText);
                        callback(adminObj);                        
                    },
                    failure: function() {
                        eventManager.fireStatusMessageEvent({ text: "Failed Getting LIMSAdmin annotations", level: "error" });
                    }
                });
}

function postSolrUpdates(jsonObj, callback) {
    var docsol = {};
    docsol["doc"] = jsonObj;
    var add = {};
    add["add"] = docsol;
    Ext.Ajax.request({
        url: "/solr/update/json?commit=true",
        method: "POST",
        jsonData: {
            add: docsol
        },
        success: function() {
            callback();
        },
        failure: function() {
            Ext.Msg.alert("Error", "Failed updating/adding record - please let Jake know:" + jsonObj);
        }
    });
}

function loadSolrSearchResult(dataSource, pagingColumns, myTranslator, index, searchResultJSON) {
	var myData = [];
        if (searchResultJSON && searchResultJSON.docs) {
            var searchResultArray = searchResultJSON.docs;
            for (var i = 0; i < searchResultArray.length; i++) {
                var myObj = searchResultArray[i];
                if (myObj.yoKey || myObj.chemKey || myObj.ypgKey || myObj.yadKey || myObj.doligoKey || myObj.loligoKey 
                || myObj.plasmidKey || myObj.yseqKey || myObj.ycgsKey || myObj.ycrKey ) {
                    myData[index] = myTranslator(myObj);
                    index = index + 1;
                }
            }
        }
        if (dataSource == "YO"){
            Dudley.lims.YOStrain.setArrayData(myData);
        }else if (dataSource == "YPG"){
            Dudley.lims.YPGStrain.setArrayData(myData);
        }else if (dataSource == "YAD"){
            Dudley.lims.YADStrain.setArrayData(myData);
        }else if (dataSource == "Chemical"){
            Dudley.lims.Chemical.setArrayData(myData);
        }else if (dataSource == "Plasmid"){
            Dudley.lims.PlasmidStrain.setArrayData(myData);
        }else if (dataSource == "AO"){
            Dudley.lims.LabOligo.setArrayData(myData);
        }else if (dataSource == "DO"){
            Dudley.lims.DeletionOligo.setArrayData(myData);
        }else if (dataSource == "YSEQ"){
            Dudley.lims.YSEQEntry.setArrayData(myData);
        }else if (dataSource == "YCGS"){
            Dudley.lims.YSEQCGS.setArrayData(myData);
        }else if (dataSource == "YCR"){
            Dudley.lims.Crossing.setArrayData(myData);
        }else if (dataSource == "YKR"){
            Dudley.lims.KruglyakCrossing.setArrayData(myData);
        }

        var myStore = new Ext.ux.data.PagingArrayStore({
            fields: pagingColumns,
            data: myData,
            sortInfo: {field:'indexNum', direction:'ASC'}
        });
        return myStore;
 }


var chemicalSolrTranslator = function(jsonSearchArray){
    var jsonObj = [];
    var chemKey = jsonSearchArray.chemKey;
    jsonObj[0] = chemKey.substring(4, chemKey.length);
    jsonObj[1] = jsonSearchArray.chemKey;
    jsonObj[2] = jsonSearchArray.chemName;
    jsonObj[3] = jsonSearchArray.chemLocation;
    jsonObj[4] = jsonSearchArray.chemReorder;
    jsonObj[5] = jsonSearchArray.chemAlias;
    jsonObj[6] = jsonSearchArray.chemKeywords;
    jsonObj[7] = jsonSearchArray.chemComments;
    return jsonObj;
};

var plasmidSolrTranslator = function(jsonSearchArray){
    var jsonObj = [];
    var plasmidKey = jsonSearchArray.plasmidKey;
    jsonObj[0] = plasmidKey.substring(2, plasmidKey.length);
    jsonObj[1] = plasmidKey;
    jsonObj[2] = jsonSearchArray.plasmidName;
    jsonObj[3] = jsonSearchArray.plasmidMarkers;
    jsonObj[4] = jsonSearchArray.plasmidYeastSelection;
    jsonObj[5] = jsonSearchArray.plasmidBoxNumber;
    jsonObj[6] = jsonSearchArray.plasmidDateEntered;
    jsonObj[7] = jsonSearchArray.plasmidBacterialHost;
    jsonObj[8] = jsonSearchArray.plasmidContributor;
    jsonObj[9] = jsonSearchArray.plasmidSavedBy;
    jsonObj[10] = jsonSearchArray.plasmidYeastOrigin;
    jsonObj[11] = jsonSearchArray.plasmidBacterialSelection;
    jsonObj[12] = jsonSearchArray.plasmidComments;
    jsonObj[13] = jsonSearchArray.plasmidReferences;
    jsonObj[14] = jsonSearchArray.plasmidPosition;
    jsonObj[15] = jsonSearchArray.plasmidAccessionId;
    jsonObj[16] = jsonSearchArray.plasmidPTA;
    jsonObj[17] = jsonSearchArray.plasmidIsbBoxNumber;
    return jsonObj;
};

var crossingSolrTranslator = function(jsonSearchArray){
    var jsonObj = [];
    var ycrKey = jsonSearchArray.ycrKey;
    jsonObj[0] = ycrKey.substring(3, ycrKey.length);
    jsonObj[1] = ycrKey;
    jsonObj[2] = jsonSearchArray.ycrAlias;
    jsonObj[3] = jsonSearchArray.ycrParentA;
    jsonObj[4] = jsonSearchArray.ycrParentAlpha;
    jsonObj[5] = jsonSearchArray.ycrProgenyCt;
    jsonObj[6] = jsonSearchArray.ycrDate;
    jsonObj[7] = jsonSearchArray.ycrNotes;
    jsonObj[8] = jsonSearchArray.ycrPlates;
    jsonObj[9] = jsonSearchArray.ycrProgenyRange;
    jsonObj[10] = jsonSearchArray.ycrIGV;
    jsonObj[11] = jsonSearchArray.ycrYCGSKeys;
    jsonObj[12] = jsonSearchArray.ycrYCGSKey1;
    jsonObj[13] = jsonSearchArray.ycrYCGSKey2;
    jsonObj[14] = jsonSearchArray.ycrSnps;
    jsonObj[15] = jsonSearchArray.ycrSnpDupes;
    jsonObj[16] = jsonSearchArray.ycrTetradCt;
   return jsonObj;
};

var sequencingSolrTranslator = function(jsonSearchArray){
    var jsonObj = [];
    var yseqKey = jsonSearchArray.yseqKey;
    jsonObj[0] = yseqKey.substring(4, yseqKey.length);
    jsonObj[1] = yseqKey;
    jsonObj[2] = jsonSearchArray.yseqDate;
    jsonObj[3] = jsonSearchArray.yseqState;
    jsonObj[4] = jsonSearchArray.yseqSampleCount;
    jsonObj[5] = jsonSearchArray.yseqReads;
    jsonObj[6] = jsonSearchArray.yseqProject;
    jsonObj[7] = jsonSearchArray.yseqContact;
    jsonObj[8] = jsonSearchArray.yseqBudget;
    jsonObj[9] = jsonSearchArray.yseqStart;
    jsonObj[10] = jsonSearchArray.yseqEnd;
    jsonObj[11] = jsonSearchArray.yseqNotes;
    jsonObj[12] = jsonSearchArray.yseqReadFormat;
    jsonObj[13] = jsonSearchArray.yseqRefGenome;
    jsonObj[14] = jsonSearchArray.yseqSamplePrep;
    jsonObj[15] = jsonSearchArray.yseqSamples;
    jsonObj[16] = jsonSearchArray.yseqSampleAlias;
    jsonObj[17] = jsonSearchArray.yseqInstrument;
    jsonObj[18] = jsonSearchArray.yseqAdaptorKit;
    jsonObj[19] = jsonSearchArray.yseqShearMethod;
    jsonObj[20] = jsonSearchArray.yseqMultiplexDegree;
    jsonObj[21] = jsonSearchArray.yseqKit;
    jsonObj[22] = jsonSearchArray.yseqSlider;
    jsonObj[23] = jsonSearchArray.yseqPrimer;
    jsonObj[24] = jsonSearchArray.yseqNFSSolid;
    return jsonObj;
};


function checkWildcard(searchtext) {
    if (searchtext.indexOf("*") != -1) {
        searchtext = searchtext.toLowerCase();
    }
    return searchtext;
}

function catTerms(op, sFields, sInput){
        var iTokens = sInput.split(op);
        var myTerms = "";
        if (op == " "){
            op = " AND ";
        }
        for (var i =0; i < iTokens.length; i++){
            if (i >= (iTokens.length -1)){
                op = "";
            }
            myTerms = myTerms + sFields + ":" + iTokens[i] + op;
        }
        return myTerms;
}
/*
Allows user to entered multiple search values, using AND/OR operation logic for the search terms
(yoFields:SPS2%20AND%20yoFields:wine)
*/
function buildSolrTerms(sFields, sInput) {
    sInput = sInput.toLowerCase();
    var terms = sFields + ":" + sInput;
    if (sInput.indexOf(" or ") != -1){
        terms = catTerms(" or ", sFields, sInput);
    }else if (sInput.indexOf(" ") != -1){
        terms = catTerms(" ", sFields, sInput);
    }
    return "(" + terms + ")";
}

function getLuceneTerm(sFields, sInput) {
    sInput = checkWildcard(sInput);
    return buildSolrTerms(sFields, sInput);
}